import request from "../utils/service";
import vue from "../main";


//登陆方法
async function login(data) {
    //添加验证码字段
    let res = await request("/user/login", data)

    //将权限集合放入vuex
    vue.$store.commit('setUser', data.name)
    vue.$store.commit('setUserPrivilege', res.data.data.privileges)
    vue.$store.commit('setWebSocketId', res.data.data.webSocketId)
    vue.$store.commit('setUserId', res.data.data.userId)

    vue.$message({ message: res.data.msg, type: 'success' })

    vue.$router.push('/home');
}


//退出登陆
async function logout() {
    //调用后台退出登陆方法
    let res = await request("/user/logout", {})

    //清空vuex
    vue.$store.commit('removeUser')

    //提示退出成功
    vue.$message({ message: res.data.msg, type: 'success' })

    //跳转到登陆页面
    vue.$router.push('/login');
}


//更新用户信息
function updateUser(data) {
    return request("/user/update", data)
}

//重置密码
function resetPassword(data) {
    return request("/user/reset/password", data)
}

//添加用户
function addUser(data) {
    return request('/user/add', data)
}

//根据用户名称检索（用于input搜索建议）
function queryUser(data) {
    return request('/user/search/name', data)
}

//批量删除用户
function batchDeleteUser(data) {
    return request('/user/delete', data)
}


//获取用户权限
function userPrivilege() {
    return request("/user/privilege", {});
}


//修改用户密码
function modifyPassword(data) {
    return request("/user/modify/password", data);
}


//返回后台用户未读信数
function unreadNum(data) {
    return request("/letter/unread/num", data);
}

//将信件标记为已读
function updateLetter(data) {
    return request("/letter/update", data);
}



//添加角色
function addRole(data) {
    return request("/role/add", data);
}

//根据角色
function updateRole(data) {
    return request("/role/update", data);
}

//批量删除角色
function batchDeleteRole(data) {
    return request("/role/delete", data)
}


//目录权限树
function menuTree() {
    return request("/menu/tree", {});
}

//添加权限
function addPrivilege(data) {
    return request("/role/add/privilege", data)
}

//获取角色对应的权限code
function rolePrivilegeCodes(data) {
    return request("/rolePrivilege/codes", data)
}


//获取组织架构列表
function organizationList(data) {
    return request("/organization/list", data)
}

//更新组织架构
function updateOrganization(data) {
    return request("/organization/update", data)
}

//添加组织架构
function addOrganization(data) {
    return request("/organization/add", data)
}

//删除组织架构
function deleteOrganization(data) {
    return request("/organization/delete", data)
}

//列出所有组织架构
function listOrganization() {
    return request('/organization/list/all', {})
}

//将用户从组件架构移除
function removeUserFormOrganization(data) {
    return request('/user/remove/organization/id', data)
}

//将用户指定为部门负责人
function assignPrinciple(data) {
    return request('/organization/assign/principle', data)
}

//添加用户到组织部门
function addUserToOrganization(data) {
    return request('/user/add/organization', data)
}

export default {

    login,
    logout,

    //用户
    addUser,
    updateUser,
    resetPassword,
    modifyPassword,
    batchDeleteUser,
    queryUser,
    unreadNum,
    updateLetter,

    //角色
    addRole,
    updateRole,
    batchDeleteRole,
    menuTree,
    addPrivilege,
    rolePrivilegeCodes,
    userPrivilege,

    //组织架构
    organizationList,
    updateOrganization,
    addOrganization,
    deleteOrganization,
    listOrganization,
    removeUserFormOrganization,
    assignPrinciple,
    addUserToOrganization,

}